﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
    public string ContainerPage = "./Answers.Edit.aspx";
    public string strError = "";
    public string strMess = "";
    public string strProcessName = "";
    public int nPermissions = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        int nPage = 1;
        string strPageID = "page_number";
        try
        {

            if (Request.QueryString[strPageID] != null)
                nPage = int.Parse(Request.QueryString[strPageID]);
        }
        catch
        {
            nPage = 1;
        }
        if (nPage <= 0) nPage = 1;

        if ((Session["logined_id"] != null) && (int.Parse(Session["logined_id"].ToString()) > 0))
        {
            if (Session["QuestionAnswer"] != null) nPermissions = int.Parse(Session["QuestionAnswer"].ToString());
            if (Session["superadmin"] != null && int.Parse(Session["superadmin"].ToString()) > 0) nPermissions = 10;
            if (nPermissions <= 0) Response.Redirect("Login.aspx");

        }
        else
            Response.Redirect("Login.aspx");
        if (!Page.IsPostBack)
        {

            if (Request.QueryString["ID"] != null)
            {
                loadDetail();
            }
            else
                txtDescripton.Visible = false;

        }

    }
    protected void loadDetail()
    {
        ContentItem obj = new ContentItem();
        obj.ID = long.Parse(Request.QueryString["ID"]);
        obj.Load(obj.ID, GV.cn, GV.trans);
        txtSummary.Text = HttpUtility.HtmlDecode(obj.Summary);
        txtDescripton.Text = obj.Detail;
        if (nPermissions <= 7)
        {
            txtDescripton.Visible = false;
            if (obj.Detail.Length > 0 || obj.CreatedByID != long.Parse(Session["logined_id"].ToString()))
                txtSummary.ReadOnly = true;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        strError = "";
        strMess = "";
        ContentItem obj = new ContentItem();
        obj.ID = -1;
        if (Request.QueryString["ID"] != null)
        {
            obj.ID = long.Parse(Request.QueryString["ID"]);
            obj.Load(obj.ID, GV.cn, GV.trans);
        }
        else
        {
            obj.CreatedByID = long.Parse(Session["logined_id"].ToString());
        }
        if (txtSummary.Text == "")
        {
            strError = "NotBlank";
            strMess = "Bạn chưa đặt câu hỏi!";
        }
        if (strError == "")
        {
            obj.Summary = HttpUtility.HtmlDecode(txtSummary.Text.Trim());
            obj.Detail = HttpUtility.HtmlDecode(txtDescripton.Text);
            if (nPermissions > 7 && obj.Detail.Trim().Length > 0)
                obj.PostedByID = long.Parse(Session["logined_id"].ToString());
            obj.Save(GV.cn, GV.trans);
            if (obj.ID == -1)
            {
                DataSet ds = DBModule.ExecuteQuery("SELECT MAX(id) from tbl_contentItem limit 0,1;", GV.cn, GV.trans);
                obj.ID = long.Parse(ds.Tables[0].Rows[0][0].ToString());
            }
            if (Request.QueryString["page_number"] != null)
                Response.Redirect("Answers.Edit.aspx?id=" + obj.ID + "&page_number=" + Request.QueryString["page_number"].ToString());
            else
                Response.Redirect("Answers.Edit.aspx?id=" + obj.ID);
        }
        else
        {
            lblInfo.Text = strMess;
        }
    }
}
